package pl.put.dcs.workManagerServer.serviceConnectors.ControlModuleServerConnectorImpl;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import java.util.logging.Level;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import org.apache.log4j.Logger;
import pl.dcs.informationModule.informationServices.stateless.flowComputingObjectElementBean.FlowComputingObjectElementBeanRemote;
import pl.dcs.informationModule.informationServices.stateless.packetBean.PacketBeanRemote;
import pl.dcs.informationModule.informationServices.stateless.projectBean.ProjectBeanRemote;
import pl.put.dcs.workManagerServer.serviceConnectors.ControlModuleServerConnectorInterface;
import pl.put.model.flowModel.flowComputingObjectElement.FlowComputingObjectElement;
import pl.put.model.packet.Packet;
import pl.put.model.project.Project;

/**
 *  @author LukaszR
 */
// <editor-fold defaultstate="collapsed" desc=" UML Marker "> 
// #[regen=yes,id=DCE.D5CAC1ED-48D5-366E-806B-5CB9C05A0507]
// </editor-fold> 
public class ControlModuleServerConnectorImpl implements ControlModuleServerConnectorInterface
{


    // <editor-fold defaultstate="collapsed" desc=" UML Marker "> 
    // #[regen=yes,id=DCE.6F6E735E-64E9-01A8-16EF-EC3154FE3F7D]
    // </editor-fold> 
    public static Logger log = Logger.getLogger( ControlModuleServerConnectorImpl.class.getName() );

    // <editor-fold defaultstate="collapsed" desc=" UML Marker "> 
    // #[regen=yes,id=DCE.1553983B-F9BB-3E6F-BD0C-EEFC7BC0C2EE]
    // </editor-fold> 
    private String serverURL;
    private InitialContext ctx;
    private PacketBeanRemote packetBean;
    private FlowComputingObjectElementBeanRemote coBean;
    private ProjectBeanRemote projectBean;

    // <editor-fold defaultstate="collapsed" desc=" UML Marker "> 
    // #[regen=yes,id=DCE.4BB0A9FB-A67A-79F5-DC4D-A93A9E698867]
    // </editor-fold> 
    public ControlModuleServerConnectorImpl()
    {
    }

    // <editor-fold defaultstate="collapsed" desc=" UML Marker "> 
    // #[regen=yes,id=DCE.30D30960-D769-1B5D-C8F5-FD4CF4BF27E9]
    // </editor-fold> 
    public List< FlowComputingObjectElement > getActualFlowComputingObjectsList()
    {
        return coBean.getActualFlowComputingObjectsList();
    }

    // <editor-fold defaultstate="collapsed" desc=" UML Marker "> 
    // #[regen=yes,id=DCE.2371DF98-C587-4C46-A160-A2540571209D]
    // </editor-fold> 
    public List<Project> getActiveProjectsList()
    {
        return projectBean.getActiveProjectsList();
    }

    // <editor-fold defaultstate="collapsed" desc=" UML Marker "> 
    // #[regen=yes,id=DCE.B1AA9668-F8B0-57EC-0AB4-4D6793311AAE]
    // </editor-fold> 
    public void connect( String url, String port )
    {
        try
        {
            Properties props = new Properties();
            
            props.load( new FileInputStream( "config/jndi.properties" ) );
            props.setProperty( "org.omg.CORBA.ORBInitialHost", url );
            props.setProperty( "org.omg.CORBA.ORBInitialPort", port );
            this.ctx = new InitialContext( props );
            projectBean = ( ProjectBeanRemote )ctx.lookup( "pl.dcs.informationModule.informationServices.stateless.projectBean.ProjectBeanRemote" );
            coBean =  ( FlowComputingObjectElementBeanRemote )ctx.lookup( "pl.dcs.informationModule.informationServices.stateless.flowComputingObjectElementBean.FlowComputingObjectElementBeanRemote" );
            packetBean = ( PacketBeanRemote )ctx.lookup( "pl.dcs.informationModule.informationServices.stateless.packetBean.PacketBeanRemote" );
        }
        catch ( NamingException ex )
        {
            java.util.logging.Logger.getLogger( ControlModuleServerConnectorImpl.class.getName() ).log( Level.SEVERE, null, ex );
        }
        catch ( IOException ex )
        {
            java.util.logging.Logger.getLogger( ControlModuleServerConnectorImpl.class.getName() ).log( Level.SEVERE, null, ex );
        }
    }


    // <editor-fold defaultstate="collapsed" desc=" UML Marker "> 
    // #[regen=yes,id=DCE.4657B4B6-AB5B-7A75-92D8-E4FA3879D0FD]
    // </editor-fold> 
    public FlowComputingObjectElement.TASK_STATUS getTaskStatus( FlowComputingObjectElement fco )
    {
        fco = coBean.getTask( fco );
        return fco.getStatus();
    }

    @Override
    public List< Packet > getTerminatedTasksIdsLimited( int number )
    {
        return packetBean.getTerminatedTasksIdsLimit( number );
    }

    public FlowComputingObjectElement storeFlowComputingObjectElement( FlowComputingObjectElement fco, int groupingId )
    {
        fco.setGroupingId( groupingId );
        return coBean.storeFlowComputingObjectElement( fco );
    }

    public int countActiveTasks( Project project )
    {
        return packetBean.countActiveTasks( project );
    }

    public int countActiveTasks( FlowComputingObjectElement fcoe )
    {
        return packetBean.countActiveTasks( fcoe );
    }

    public void setPacketStatusStarted( Packet packet )
    {
        packetBean.setPacketStatusStarted( packet );
    }

    public void setPacketStatusWaited( Packet packet )
    {
        packetBean.setPacketStatusWaited( packet );
    }


    public Packet addPacket( Packet packet )
    {
        packet.setId( null );
        return packetBean.addPacket( packet );
    }

    public List<Project> getProjectList()
    {
        return projectBean.getProjectList();
    }

    public void removeProject( Project p )
    {
        projectBean.removeProject( p );
    }

    public Project addProject( Project project )
    {
        return projectBean.addProject( project );
    }

    
}
